package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserLotteryRecordsBo;
import org.dromara.system.domain.vo.PrizeDrawVo;
import org.dromara.system.domain.vo.QueryLotteryInfoVo;
import org.dromara.system.domain.vo.SysUserLotteryRecordsVo;
import org.dromara.system.enums.MachineConstant;
import org.dromara.system.service.ISysUserLotteryRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 抽奖记录
 *
 * @author Lion Li
 * @date 2025-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userLotteryRecords")
public class SysUserLotteryRecordsController extends BaseController {

    private final ISysUserLotteryRecordsService sysUserLotteryRecordsService;

    /**
     * 查询抽奖记录列表
     */
//    @SaCheckPermission("system:userLotteryRecords:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserLotteryRecordsVo> list(SysUserLotteryRecordsBo bo, PageQuery pageQuery) {
        return sysUserLotteryRecordsService.queryPageList(bo, pageQuery);
    }

    //公共抽奖
    @GetMapping("/listCommon")
    public TableDataInfo<SysUserLotteryRecordsVo> listCommon(SysUserLotteryRecordsBo bo, PageQuery pageQuery) {
        bo.setUserId(MachineConstant.ADMIN);
        return sysUserLotteryRecordsService.queryPageList(bo, pageQuery);
    }

    //前端抽奖记录
    @GetMapping("/listReception")
    public TableDataInfo<SysUserLotteryRecordsVo> listReception(SysUserLotteryRecordsBo bo, PageQuery pageQuery) {
        return sysUserLotteryRecordsService.listReception(bo, pageQuery);
    }

    /**
     * 导出抽奖记录列表
     */
//    @SaCheckPermission("system:userLotteryRecords:export")
    @Log(title = "抽奖记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserLotteryRecordsBo bo, HttpServletResponse response) {
        List<SysUserLotteryRecordsVo> list = sysUserLotteryRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "抽奖记录", SysUserLotteryRecordsVo.class, response);
    }

    /**
     * 获取抽奖记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userLotteryRecords:query")
    @GetMapping("/{id}")
    public R<SysUserLotteryRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long id) {
        return R.ok(sysUserLotteryRecordsService.queryById(id));
    }

    /**
     * 新增抽奖记录
     */
//    @SaCheckPermission("system:userLotteryRecords:add")
    @Log(title = "抽奖记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserLotteryRecordsBo bo) {
        return toAjax(sysUserLotteryRecordsService.insertByBo(bo));
    }

    /**
     * 修改抽奖记录
     */
//    @SaCheckPermission("system:userLotteryRecords:edit")
    @Log(title = "抽奖记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserLotteryRecordsBo bo) {
        return toAjax(sysUserLotteryRecordsService.updateByBo(bo));
    }

    /**
     * 删除抽奖记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userLotteryRecords:remove")
    @Log(title = "抽奖记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserLotteryRecordsService.deleteWithValidByIds(List.of(ids), true));
    }

    @PostMapping("/prizeDraw")
    @RepeatSubmit(interval = 10, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    public R<PrizeDrawVo> prizeDraw() {
        return R.ok(sysUserLotteryRecordsService.prizeDraw());
    }


    //查询当前剩余抽奖积分和每次抽奖消耗多少抽奖积分
    @PostMapping("/queryLotteryInfo")
    public R<QueryLotteryInfoVo> queryLotteryInfo() {
        return R.ok(sysUserLotteryRecordsService.queryLotteryInfo());
    }

}
